package org.duncan.sjge.gamedata.sounds;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.duncan.sjge.gamedata.sprites.Sprite;
import org.duncan.sjge.gamedata.sprites.SpriteMgr;
import javax.sound.sampled.UnsupportedAudioFileException;

/**
 *
 * @author Duncan
 */
public class SoundMgr {
    private Map<File, Sound> mSounds = new HashMap<>();
    
    public Sound get(final File file) {
        if (!mSounds.containsKey(file)) {
            System.out.println("Loading sound '" + file.getName() + "'.");
            try {
                mSounds.put(file, new Sound(file));
            } catch (final IOException ex) {
                System.err.println("Error! Could not find sound file '" + file + "'.");
                ex.printStackTrace();
                System.exit(1);
            } catch (final UnsupportedAudioFileException ex) {
                System.err.println("Error! Unsupported audio format '." + file.getAbsolutePath().substring(file.getAbsolutePath().lastIndexOf(".")) + "'.");
                ex.printStackTrace();
                System.exit(1);
            }
        }
        return mSounds.get(file);
    }
    
    // Singleton
    private static SoundMgr inst;
    public static SoundMgr instance() {
        if (inst == null) {inst = new SoundMgr();}
        return inst;
    }
}
